package de.XPortLib;

class Command {
	/** Number of bytes in a Command for the B-Module */
	public static final int CMD_LENGTH = 8;

	/** Request the Relais-States */
	public static final int CMD_READRELAIS = 0x81;

	/** Command to set relais */
	public static final int CMD_SETRELAIS = 0x80;

	/** Command to set date */
	public static final int CMD_SETDATE = 0x45;

	/** Command to set time */
	public static final int CMD_SETTIME = 0x44;

	/** Read an Event from B-Module */
	public static final int CMD_GETEVENT = 0x46;

	/** Read an Event from B-Module */
	public static final int CMD_SETEVENT = 0x47;

	/** Delete an Event from B-Module */
	public static final int CMD_DELETEEVENT = 0x48;

	/** Ask B-Module for 64 Samples */
	public static final int CMD_READADC_64 = 0x43;

	/** this value is just a placeholder to encode XPort specific values */
	public static final int START_CODE = 0x41; /* 'A' */

	/** this value is just a placeholder to encode XPort specific values */
	public static final int STOP_CODE = 0x42; /* 'B' */
	
	/** stores the command which was created out of the parts */
	private int[] mFullCommand;
	
	/** the basic command code */
	private int mCommand;
	
	/** stores the answer which is returned by the XPort */
	private int[] mAnswer;
	
	/** stores additional data which might be returned */
	private int[] mRetour;
	
	/** determines how many bytes additional retour-data is expected */
	private int mExpectedRetourSize;
	
	/** determines additional data to be sent after a command was sent */
	private int[] mPayload;
	
	/**
	 * the default constructor
	 * @param command
	 * 			the basic command code
	 */
	public Command(int command){
		this(command, null);
	}
	
	/**
	 * Constructor
	 * @param command
	 * 			the basic command code
	 * @param data
	 * 			data to be sent with the command
	 */
	public Command(int command, int[] data){
		this(command, data, 0);
	}
	
	/**
	 * Constructor
	 * @param command
	 * 			the basic command code
	 * @param data
	 * 			data to be sent with the command
	 * @param expectedRetourSize
	 * 			determines how many bytes additional retour-data is expected
	 */
	public Command(int command, int[] data, int expectedRetourSize){
		this(command, data, expectedRetourSize, null);
	}
	
	/**
	 * Constructor
	 * @param command
	 * 			the basic command code
	 * @param data
	 * 			data to be sent with the command
	 * @param expectedRetourSize
	 * 			determines how many bytes additional retour-data is expected
	 * @param payload
	 * 			determines additional data to be sent after a command was sent
	 */
	public Command(int command, int[] data, int expectedRetourSize, int[] payload){
		mFullCommand = new int[CMD_LENGTH];
		mCommand = command;
		mAnswer = null;
		mRetour = null;
		mExpectedRetourSize = expectedRetourSize;
		mPayload = payload;
		
		int length;

		if (data != null)
			length = data.length;
		else
			length = 0;

		// construct Command
		mFullCommand[0] = START_CODE;
		mFullCommand[CMD_LENGTH - 1] = STOP_CODE;
		mFullCommand[1] = command;
		for (int i = 2; i < CMD_LENGTH - 1; i++)
			if (i - 2 < length)
				mFullCommand[i] = data[i - 2];
			else
				mFullCommand[i] = 0;
	}


	/**
	 * default getter method 
	 * @return 
	 * 		the answer which was returned by the XPort
	 */
	public int[] getAnswer() {
		return mAnswer;
	}

	/**
	 * sets the answer 
	 * @param answer
	 * 			answer which was returned by the XPort
	 */
	public void setAnswer(int[] answer) {
		mAnswer = answer;
	}

	/**
	 * default getter method 
	 * @return 
	 * 		the retour-data which was returned by the XPort
	 */
	public int[] getRetour() {
		return mRetour;
	}

	/**
	 * sets the retour-data 
	 * @param retour
	 * 			retour-data which was returned by the XPort
	 */
	public void setRetour(int[] retour) {
		mRetour = retour;
	}

	/**
	 * default getter method
	 * @return
	 * 			the fullcommand which was created by the constructor
	 */
	public int[] getFullCommand() {
		return mFullCommand;
	}

	/**
	 * default getter method
	 * @return
	 * 			the basic command code which was passed to the constructor
	 */
	public int getCommand() {
		return mCommand;
	}


	/**
	 * default getter method
	 * @return
	 * 			the payload-data which was passed to the constructor
	 */
	public int[] getPayload() {
		return mPayload;
	}

	/**
	 * default getter method
	 * @return
	 * 			the expected retour-size which was passed to the constructor
	 */
	public int getExpectedRetourSize() {
		return mExpectedRetourSize;
	}
}
